<template>
  <div class="bind-card">
    <TopNavBar type="back" :title="_t18('sidebar_language')" />
    
    <div class="content">
      <van-radio-group v-model="checked">
        <van-radio
          :name="index"
          shape="square"
          v-for="(item, index) in languageList"
          :key="index"
          label-position="left"
          :checked-color="'#17AC74'"
          @click="setLanguage(item)"
        >
          <image-load 
            :filePath="`/common/lang/${item.dictValue}.png`" 
            alt="" 
            width="20" 
            class="nation" 
            v-if="item.imgUrl" 
          />
          <svg-load v-else :name="item.dictValue" class="nation"></svg-load>
          {{ item.remark }}
          <template #icon="props">
            <img :src="props.checked ? currentIcon : noCuttemdIcon" alt="icon" width="25" height="25" />
          </template>
        </van-radio>
      </van-radio-group>
    </div>

    <div class="confirm-wrap">
      <van-button type="primary" class="confirm-btn" @click="goHome">{{ _t18('btnConfirm') }}</van-button>
    </div>
  </div>
</template>

<script setup>
import { storageDict } from '@/config/dict'
import { useMainStore } from '@/store/index.js'
import { computed, ref } from 'vue'
import currentIcon from '@/assets/defi/current.png'
import noCuttemdIcon from '@/assets/defi/noCuttemd.png'
import { useRouter } from 'vue-router'

const mainStore = useMainStore()
const router = useRouter()

const current = ref(localStorage.getItem(storageDict.LANGUAGE))

const setLanguage = (item) => {
  mainStore.setLanguage(item.dictValue)
  current.value = item.dictValue
}

const checked = computed(() => {
  let temp = 0
  mainStore.languageList.forEach((item, index) => {
    if (item.dictValue == current.value) {
      temp = index
    }
  })
  return temp
})

const languageList = mainStore.languageList

const goHome = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
* {
  color: var(--ex-default-font-color);
}
.content {
  padding: 0 15px;
  text-align: left;
  & > div {
    font-size: 16px;
  }
  .van-radio {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 0 10px 0;
    border-bottom: 1px solid #E5E5E5;
    :deep(.van-radio__label) {
      font-size: 14px;
      color: var(--ex-default-font-color);
    }
  }
  .nation {
    font-size: 24px;
    margin-right: 20px;
  }
}

.confirm-wrap {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}
.confirm-btn {
  width: 90%;
  background: #000;
  color: #fff;
  border: none;
  border-radius: 999px;   /* 半圆效果 */
  height: 45px;            /* 可以自定义高度，更好看 */
  font-size: 16px;
}
</style>
